//
//  ChatRoomMemberViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/4/6.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "ChatRoomMemberViewController.h"
#import "CeShiModelOfFriendMessage.h"
#import "FriendMessageTableViewCell.h"
#import "SearchChatRoomMemberViewController.h"
#import "MyChatViewController.h"

@interface ChatRoomMemberViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *chatRoomMemberTableView;
@property (nonatomic, strong) NSMutableArray *chatRoomMemberDataArray;

@end

@implementation ChatRoomMemberViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // navi的修改
    [self changeNavigation];
    // 建立数据源
    [self addData];
    // 布局
    [self addMySubview];

}


// navi的修改
- (void)changeNavigation
{
    [self.navigationController.navigationBar setTranslucent:NO];
    self.navigationItem.title = @"聊天室成员列表";
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
    
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 建立数据源
- (void)addData
{
    // 测试数据
    NSDictionary *dicFriend1 = @{@"_friendImage": @"", @"_name": @"我叫张国文", @"_gender": @"男", @"_myID": @"987654321", @"_mark": @[@"健康达人", @"我爱看电影你呢"]};
    NSDictionary *dicFriend2 = @{@"_friendImage": @"", @"_name": @"带秋天", @"_gender": @"女", @"_myID": @"123456789", @"_mark": @[@"我是运动达人", @"爱看电影"]};
 
    NSMutableArray *dataArray = [NSMutableArray arrayWithObjects:dicFriend1, dicFriend2, nil];
    
    self.chatRoomMemberDataArray = [NSMutableArray array];
    for (NSDictionary *tempDic in dataArray) {
        CeShiModelOfFriendMessage *model = [[CeShiModelOfFriendMessage alloc] init];
        [model setValuesForKeysWithDictionary:tempDic];
        [self.chatRoomMemberDataArray addObject:model];
    }
    
}

#pragma mark - 布局
- (void)addMySubview
{
    self.chatRoomMemberTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    self.chatRoomMemberTableView.backgroundColor = [UIColor whiteColor];
    self.chatRoomMemberTableView.dataSource = self;
    self.chatRoomMemberTableView.delegate = self;
    [self.view addSubview:_chatRoomMemberTableView];
    
    // 构造表头
    UIView *whiteBackHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 60 * kAdjustParamater)];
    whiteBackHeaderView.userInteractionEnabled = YES;
    whiteBackHeaderView.backgroundColor = [UIColor whiteColor];
    self.chatRoomMemberTableView.tableHeaderView = whiteBackHeaderView;
    
    UIImageView *searchImageV = [[UIImageView alloc] initWithFrame:CGRectMake(16 * kAdjustParamater, 17 * kAdjustParamater, 20 * kAdjustParamater, 20 * kAdjustParamater)];
    searchImageV.image = [UIImage imageNamed:@"tianjiahaoyoutubiao-tianjia"];
    [whiteBackHeaderView addSubview:searchImageV];
    
    UILabel *searchMemberLabel = [[UILabel alloc] initWithFrame:CGRectMake(searchImageV.right + 10 * kAdjustParamater, 20 * kAdjustParamater, 100 * kAdjustParamater, 11 * kAdjustParamater)];
    searchMemberLabel.userInteractionEnabled = YES;
    searchMemberLabel.text = @"输入成员名字/ID";
    searchMemberLabel.font = [UIFont systemFontOfSize:k13Big];
    searchMemberLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [whiteBackHeaderView addSubview:searchMemberLabel];
    
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(searchMember:)];
    [searchMemberLabel addGestureRecognizer:tapGes];

    UILabel *grayLabelOnHeaderBottom = [[UILabel alloc] initWithFrame:CGRectMake(0, 50 * kAdjustParamater, kScreenWidth, 10 * kAdjustParamater)];
    grayLabelOnHeaderBottom.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.05];
    [whiteBackHeaderView addSubview:grayLabelOnHeaderBottom];
}

#pragma mark - 搜索
- (void)searchMember:(UITapGestureRecognizer *)tapGes
{
    NSLog(@"搜索");
    SearchChatRoomMemberViewController *searchChatRoomMemberVC = [[SearchChatRoomMemberViewController alloc] init];
    UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:searchChatRoomMemberVC];
    [self presentViewController:naVC animated:YES completion:nil];
}

// tableView代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.chatRoomMemberDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    FriendMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[FriendMessageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    CeShiModelOfFriendMessage *model = self.chatRoomMemberDataArray[indexPath.row];
    cell.model = model;
    [cell.attentionButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    cell.attentionButton.tag = 250;
    [cell.addFriendButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    cell.addFriendButton.tag = 251;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 75 * kAdjustParamater;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    CeShiModelOfFriendMessage *friendModel = self.chatRoomMemberDataArray[indexPath.row];
    NSLog(@"姓名= %@", friendModel.name);
    
    MyChatViewController *myChatVC = [[MyChatViewController alloc] init];
    myChatVC.conversationType = ConversationType_PRIVATE;
    myChatVC.title = friendModel.name;
    [self.navigationController pushViewController:myChatVC animated:YES];
}

#pragma mark - 点击cell上的button
- (void)clickButton:(UIButton *)button
{
    UIView *view = [button superview];
    FriendMessageTableViewCell *cell = (FriendMessageTableViewCell *)[view superview];
    NSIndexPath *indexPath = [self.chatRoomMemberTableView indexPathForCell:cell];

    
    switch (button.tag) {
        case 250:
        {
            NSLog(@"row = %ld, 关注", indexPath.row);
            break;
        }
            case 251:
        {
            NSLog(@"row = %ld, 好友", indexPath.row);
            break;
        }
        default:
            break;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
